package com.cxhc.app.api;

import com.cxhc.app.api.vo.HistoryApprovalVo;
import com.cxhc.common.core.controller.BaseController;
import com.cxhc.common.core.page.TableDataInfo;
import com.cxhc.system.domain.app.ApprovalFlow;
import com.cxhc.system.domain.app.GroupUser;
import com.cxhc.system.domain.app.RpRevHandling;
import com.cxhc.system.dto.ApprovalDto;
import com.cxhc.system.dto.ApprovalNoPassDto;
import com.cxhc.system.dto.ApprovalPassDto;
import com.cxhc.system.dto.EventApprovalDto;
import com.cxhc.app.api.vo.EventApprovalVo;
import com.cxhc.common.core.domain.R;
import com.cxhc.system.domain.app.EventApproval;
import com.cxhc.system.service.app.IApprovalFlowService;
import com.cxhc.system.service.app.IEventApprovalFileService;
import com.cxhc.system.service.app.IEventApprovalService;
import com.cxhc.system.service.app.IGroupUserService;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事项报备审批
 */
@RestController
@RequestMapping("/api/eventApproval")
public class ApiEventApproval extends BaseController {
    @Resource
    private IEventApprovalService eventApprovalService;

    @Resource
    private IGroupUserService groupUserService;

    @Resource
    private IEventApprovalFileService approvalFileService;

    @Resource
    private IApprovalFlowService approvalFlowService;


    /**
     * 列表
     *
     * @param vo
     * @return
     */
    @GetMapping("/list")
    public R<List<EventApproval>> approvalList(EventApprovalVo vo) {
        return R.ok(eventApprovalService.selectEventApprovalList(new EventApproval().setTitle(vo.getName()).setStatus(vo.getStatus())));
    }

    /**
     * 列表
     *
     * @return
     */
    @GetMapping("/oldList")
    public TableDataInfo approvalOldList(HistoryApprovalVo vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EventApproval> list = eventApprovalService.selectEventApprovalOldList(vo.getUserName(), vo.getStatus());
        return getDataTable(list);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R<Map<String, Object>> approvalInfo(@PathVariable Long id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("info", eventApprovalService.selectEventApprovalById(id));
        map.put("approvalUserList",  approvalFlowService.selectApprovalFlowListByEventApprovalId(id));
        map.put("fileList",  approvalFileService.selectEventApprovalFileListByEventApprovalId(id));
        map.put("nextApproval", approvalFlowService.selectNextApprovalByEventApprovalId(id));
        return R.ok(map);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/oldInfo/{id}")
    public R<Map<String, Object>> approvalOldInfo(@PathVariable Long id) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalOldById(id);
        String[] fileUrls = eventApproval.getFile().split(",");
        return R.ok(ImmutableMap.of("info", eventApproval,
                "approvalUserList", approvalFlowService.selectApprovalFlowOldListByEventApprovalId(id),
                "fileList", fileUrls));
    }

    /**
     * 审批人员列表
     *
     * @return
     */
    @GetMapping("/approvalUserList")
    public R<List<GroupUser>> approvalUserList() {
        return R.ok(groupUserService.selectGroupUserList(new GroupUser()));
    }


    /**
     * 新增报备审批
     *
     * @param dto
     * @return
     */
    @PostMapping("/postEventApproval")
    private R<String> postEventApproval(@RequestBody @Valid EventApprovalDto dto) {
        int row = eventApprovalService.insert(dto);
        return row == 1 ? R.ok() : R.fail();
    }


    /**
     * 审批
     *
     * @param dto
     */
    @PutMapping("/approval")
    private R<String> approval(@RequestBody @Valid ApprovalDto dto) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalById(dto.getId());
        if (eventApproval == null) {
            return R.fail("事项信息不存在");
        }
        if (eventApproval.getStatus() == 2) {
            return R.fail("事项已审批");
        }
        int row = eventApprovalService.updateEventApproval(eventApproval.setStatus(dto.getStatus()).setContent(dto.getContent()));

        if (row == 1 && dto.getUserId() != null) {
            approvalFlowService.insertApprovalFlow(new ApprovalFlow()
                    .setEventId(dto.getId())
                    .setUserId(dto.getUserId())
                    .setDeptName(dto.getDeptName())
                    .setUserName(dto.getUserName()));
        }
        return row == 1 ? R.ok() : R.fail();
    }


    /**
     * 审批通过
     *
     * @param dto
     */
    @PutMapping("/approvalPass")
    private R<String> approvalPass(@RequestBody @Valid ApprovalPassDto dto) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalById(dto.getId());
        if (eventApproval == null) {
            return R.fail("事项信息不存在");
        }
        if (eventApproval.getStatus() == 2 || eventApproval.getStatus() == -1) {
            return R.fail("事项已审批");
        }
        // (本次)审批流程对象
        ApprovalFlow approvalFlow = approvalFlowService.selectApprovalFlowById(dto.getFlowId());
        if (approvalFlow == null) {
            return R.fail("事项审批流程不存在");
        }
        // 审批中
        int status = 1;
        if (dto.getUserId() == null) {
            status = 2;
        }
        // 更新状态
        int row = eventApprovalService.updateEventApproval(eventApproval.setStatus(status));
        if (row == 1) {
            approvalFlowService.updateApprovalFlow(approvalFlow.setContent(dto.getContent()).setStatus(2).setApprovalAt(new Date()));
            if (dto.getUserId() != null) {
                approvalFlowService.insertApprovalFlow(new ApprovalFlow()
                        .setEventId(dto.getId())
                        .setUserId(dto.getUserId())
                        .setDeptName(dto.getDeptName())
                        .setUserName(dto.getUserName()));
            }
        }
        return row == 1 ? R.ok() : R.fail();
    }


    /**
     * 审批不通过
     *
     * @param dto
     */
    @PutMapping("/approvalNoPass")
    private R<String> approvalNoPass(@RequestBody @Valid ApprovalNoPassDto dto) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalById(dto.getId());
        if (eventApproval == null) {
            return R.fail("事项信息不存在");
        }
        if (eventApproval.getStatus() == 2 || eventApproval.getStatus() == -1) {
            return R.fail("事项已审批");
        }
        // (本次)审批流程对象
        ApprovalFlow approvalFlow = approvalFlowService.selectApprovalFlowById(dto.getFlowId());
        if (approvalFlow == null) {
            return R.fail("事项审批流程不存在");
        }
        // 更新状态
        int row = eventApprovalService.updateEventApproval(eventApproval.setStatus(-1));
        if (row == 1) {
            approvalFlowService.updateApprovalFlow(approvalFlow.setContent(dto.getContent()).setStatus(-1).setApprovalAt(new Date()));
        }
        return row == 1 ? R.ok() : R.fail();
    }


}
